﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Soveryn.Commerce.API;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Pages
{
    public partial class PayPalSimpleIPN : PageBaseClass
    {

        protected new void Page_PreInit(object sender, EventArgs e)
        {
            Website = WebsiteData.WebsiteDetail();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                String payPalURL = "https://www.paypal.com/cgi-bin/webscr";
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(payPalURL);

                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                byte[] param = Request.BinaryRead(Request.ContentLength);
                string strRequest = Encoding.ASCII.GetString(param);
                string ipnPost = strRequest;
                strRequest += "&cmd=_notify-validate";
                req.ContentLength = strRequest.Length;


                StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
                streamOut.Write(strRequest);
                streamOut.Close();

                StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
                string strResponse = streamIn.ReadToEnd();
                streamIn.Close();

                if (strResponse == "VERIFIED")
                {

                    Dictionary<string, object> args = new Dictionary<string, object>();
                    args.Add("WebsiteAPIKey", PageBaseClass.WebsiteAPIKey);
                    args.Add("Data", StringUtils.EncodeTo64(strRequest));
                    APIEnvelope env = Soveryn.Commerce.Shared.API.Call(PageBaseClass.CustomerAPIURL, "PayPalUpdateTransaction", args, false, false);

                    // MiscUtils.ErrorLog(new Exception(), paymentID + " - " + orderID + "     " + custom);
                    //check the payment_status is Completed
                    //check that txn_id has not been previously processed
                    //check that receiver_email is your Primary PayPal email
                    //check that payment_amount/payment_currency are correct
                    //process payment

                    MiscUtils.ErrorLog(new Exception(), strResponse + " - " + strRequest);


                }
                else if (strResponse == "INVALID")
                {
                    //log for manual investigation
                    MiscUtils.Log("PayPalSimpleIPN - INVALID - response:" + strResponse + " request:" + strRequest);
                }
                else
                {
                    //log response/ipn data for manual investigation
                    MiscUtils.Log("PayPalSimpleIPN - UNKNOWN - response:" + strResponse + " request:" + strRequest);
                }
            }
            catch (Exception ex)
            {
                MiscUtils.ErrorLog(ex, "PayPalSimpleIPN - WebsiteID:" + Website.WebsiteID);
                throw;
            }
        }
    }
}